﻿using DataCollection.Infrastructure;
using DataCollection.WinForm.ViewModel;
using Masuit.Tools;

namespace DataCollection.WinForm.Forms
{
    public partial class AddTemplateForm : DCBaseForm
    {
        #region 属性

        private TemplateViewModel viewModel = null!;

        /// <summary>
        /// 需要绑定的控件
        /// </summary>
        private Dictionary<string, Control> ControlDict = new Dictionary<string, Control>();

        #endregion

        #region 自定义方法

        #endregion

        #region 系统事件
        public AddTemplateForm()
        {
            InitializeComponent();
            Header.Text = "添加试压模板";
            ControlDict.Add("number", NumberInput);
            ControlDict.Add("pipePosition", PipePositionSelect);
            ControlDict.Add("designPressure", DesignPressureInput);
            ControlDict.Add("errorRange", ErrorRangeInput);

            ControlDict.Add("firstStagePressure", FirstStagePressureInput);
            ControlDict.Add("firstStageTime", FirstStageTimeInput);
            ControlDict.Add("firstStageTimeUnit", FirstStageTimeUnitSelect);
            ControlDict.Add("firstStageEstimateWay", FirstStageEstimateWaySelect);
            ControlDict.Add("firstStageEstimateValue", FirstStageEstimateValueInput);

            ControlDict.Add("secondStagePressure", SecondStagePressureInput);
            ControlDict.Add("secondStageTime", SecondStageTimeInput);
            ControlDict.Add("secondStageTimeUnit", SecondStageTimeUnitSelect);
            ControlDict.Add("secondStageEstimateWay", SecondStageEstimateWaySelect);
            ControlDict.Add("secondStageEstimateValue", SecondStageEstimateValueInput);

            ControlDict.Add("thirdStagePressure", ThirdStagePressureInput);
            ControlDict.Add("thirdStageTime", ThirdStageTimeInput);
            ControlDict.Add("thirdStageTimeUnit", ThirdStageTimeUnitSelect);
            ControlDict.Add("thirdStageEstimateWay", ThirdStageEstimateWaySelect);
            ControlDict.Add("thirdStageEstimateValue", ThirdStageEstimateValueInput);

            ControlDict.Add("intensityStagePressure", IntensityStagePressureInput);
            ControlDict.Add("intensityStageTime", IntensityStageTimeInput);
            ControlDict.Add("intensityStageTimeUnit", IntensityStageTimeUnitSelect);
            ControlDict.Add("intensityStageEstimateWay", IntensityStageEstimateWaySelect);
            ControlDict.Add("intensityStageEstimateValue", IntensityStageEstimateValueInput);

            ControlDict.Add("rigorStagePressure", RigorStagePressureInput);
            ControlDict.Add("rigorStageTime", RigorStageTimeInput);
            ControlDict.Add("rigorStageTimeUnit", RigorStageTimeUnitSelect);
            ControlDict.Add("rigorStageEstimateWay", RigorStageEstimateWaySelect);
            ControlDict.Add("rigorStageEstimateValue", RigorStageEstimateValueInput);
        }

        private void AddTemplateForm_Load(object sender, EventArgs e)
        {
            viewModel = new TemplateViewModel(DateTimeOffset.Now.ToUnixTimeSeconds());
            foreach (var item in ControlDict)
            {
                Bindings(item.Value, "Text", item.Key, viewModel);
            }
        }
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (viewModel.Number.ToString().IsNullOrEmpty())
            {
                Error("请输入模板编号");
                return;
            }

            if (viewModel.PipePosition.IsNullOrEmpty())
            {
                Error("请选择管道位置");
                return;
            }

            if (viewModel.DesignPressure.ToString().IsNullOrEmpty())
            {
                Error("请输入设计压力");
                return;
            }

            try
            {
                if (double.Parse(FirstStagePressureInput.Text) > 0)
                {
                    if (double.Parse(FirstStageTimeInput.Text) <= 0)
                    {
                        Error("请输入阶段一持续时间");
                        return;
                    }

                    if (FirstStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        Error("请选择阶段一时间单位");
                        return;
                    }

                    if (FirstStageEstimateWaySelect.Text.IsNullOrEmpty())
                    {
                        Error("请选择阶段一判断方式");
                        return;
                    }

                    if (double.Parse(FirstStageEstimateValueInput.Text) <= 0)
                    {
                        Error("请输入阶段一判断数值");
                        return;
                    }
                }
            }
            catch (Exception)
            {

            }

            viewModel.Apply();
            Response response = viewModel.Add();
            if (response.Code == Response.SuccessCode)
            {
                if (this.Owner != null)
                {
                    ManageTemplateForm frm = (ManageTemplateForm)this.Owner;
                    frm.LoadList();
                    frm.Success(response.Msg);
                    Singleton.Instance.ReloadTemplateList();
                }
                this.Close();
            }
            else
            {
                Error(response.Msg);
            }
        }

        private void StagePressureInput_TextChanged(object sender, EventArgs e)
        {
            AntdUI.Input input = (AntdUI.Input)sender;
            try
            {
                double pressure = double.Parse(input.Text);
                bool enable = false;
                if (pressure > 0)
                {
                    enable = true;
                }
                if (input == FirstStagePressureInput)
                {
                    FirstStagePanel.Enabled = enable;
                }
                else if (input == SecondStagePressureInput)
                {
                    SecondStagePanel.Enabled = enable;
                }
                else if (input == ThirdStagePressureInput)
                {
                    ThirdStagePanel.Enabled = enable;
                }
                else if (input == IntensityStagePressureInput)
                {
                    IntensityStagePanel.Enabled = enable;
                }
                else if (input == RigorStagePressureInput)
                {
                    RigorStagePanel.Enabled = enable;
                }
            }
            catch (Exception)
            {
                Error("请输入正确的压力值");
                return;
            }
        }

        private void Input_Click(object sender, EventArgs e)
        {
            AntdUI.Input input = (AntdUI.Input)sender;
            KeyBoardForm.input = input;
            KeyBoardForm frm = new KeyBoardForm();
            frm.ShowDialog(this);
        }
        #endregion
    }
}
